package weaver.splittable;

import org.apache.log4j.Logger;
import weaver.splittable.biz.STBizConfig;
import weaver.splittable.biz.STBizDataCleaner;
import weaver.splittable.biz.STBizInitializer;
import weaver.splittable.biz.STBizInterceptor;
import weaver.splittable.db.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 分表容器
 * 主要用来缓存一些分别组件随时需要用到的对象
 * author: YUEYANG 2023-01-12
 */
public class STContext {

    private static final Logger LOGGER = Logger.getLogger(STConfig.LOGGER_NAME);

    // 存储所有需要分表的表
    private static Map<String, String> splitTables = new LinkedHashMap<>();

    // 存储所有的业务初始化器
    private static Map<String, STBizInitializer> bizInitializers = new HashMap<>();

    // 存储所有的业务拦截器
    private static Map<String, STBizInterceptor> bizInterceptors = new HashMap<>();

    // 存储所有的业务数据清理器
    private static Map<String, STBizDataCleaner> bizDataCleaners = new HashMap<>();

    // DB 工具
    private static STCreateIndexUtil createIndexUtil;
    private static STCreateTableUtil createTableUtil;
    private static STCreateTriggerUtil createTriggerUtil;
    private static STSyncClearSqlUtil syncSqlUtil;

    static {

        // 初始化DB工具
        if (STDBSupport.isSupport(STConfig.DB_TYPE)) {
            createIndexUtil = STDBSupport.getCreateIndexUtil(STConfig.DB_TYPE);
            createTableUtil = STDBSupport.getCreateTableUtil(STConfig.DB_TYPE);
            createTriggerUtil = STDBSupport.getCreateTriggerUtil(STConfig.DB_TYPE);
            syncSqlUtil = STDBSupport.getSyncSqlUtil(STConfig.DB_TYPE);
        }

        // 初始化分表业务模块
        for (String bizInitializerClassName : STConfig.BIZ_INITIALIZER_SET) {
            try {
                Class<?> bizInitializerClass = Class.forName(bizInitializerClassName);
                STBizInitializer bizInitializer = (STBizInitializer) bizInitializerClass.newInstance();
                STBizConfig bizConfig = bizInitializer.register();
                bizRegister(bizConfig);
                bizInitializers.put(bizConfig.getBizName(), bizInitializer);
                LOGGER.info("Registered BizConfig: " + bizConfig.getBizName());
            } catch (Exception e) {
                LOGGER.error("STContext register BizConfig error:", e);
            }
        }
    }

    public static void bizRegister(String bizName, Map<String, String> sts, STBizInterceptor bizInterceptor, STBizDataCleaner bizDataCleaner) {
        splitTables.putAll(sts);
        bizInterceptors.put(bizName, bizInterceptor);
        bizDataCleaners.put(bizName, bizDataCleaner);
    }

    public static void bizRegister(STBizConfig bizConfig) {
        bizRegister(bizConfig.getBizName(), bizConfig.getSplitTables(), bizConfig.getBizInterceptor(), bizConfig.getBizDataCleaner());
    }

    public static Map<String, String> SPLIT_TABLES() {
        return splitTables;
    }

    public static Collection<STBizInterceptor> BIZ_INTERCEPTORS() {
        return bizInterceptors.values();
    }

    public static Collection<STBizInitializer> BIZ_INITIALIZERS() {
        return bizInitializers.values();
    }

    public static Collection<STBizDataCleaner> BIZ_DATA_CLEANER() {
        return bizDataCleaners.values();
    }


    public static STBizInterceptor BIZ_INTERCEPTORS(String bizName) {
        return bizInterceptors.get(bizName);
    }

    public static STBizInitializer BIZ_INITIALIZERS(String bizName) {
        return bizInitializers.get(bizName);
    }

    public static STBizDataCleaner BIZ_DATA_CLEANER(String bizName) {
        return bizDataCleaners.get(bizName);
    }



    public static STCreateIndexUtil CREATE_INDEX() {
        return createIndexUtil;
    }

    public static STCreateTableUtil CREATE_TABLE() {
        return createTableUtil;
    }

    public static STCreateTriggerUtil CREATE_TRIGGER() {
        return createTriggerUtil;
    }

    public static STSyncClearSqlUtil SYNC_SQL() {
        return syncSqlUtil;
    }

}
